package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;

import options.AllgemeinPanel;
import options.KalenderOptionPanel;
import options.KontakteOptionPanel;
import options.MailOptionPanel;
import options.ToDoOptionPanel;
import tools.OptionReader;
import tools.OptionWriter;

/**
 * Stellt Optionsmenü bereit
 * 
 * @author Michi
 * 
 */
public class OptionsTest extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6251620434503742468L;
	private JPanel contentPane;
	private OptionsTest optionsTestInstance;
	private String StartLine = "";// Testen ob es ein Optionsfile ist
	private String calOptStandardView = ""; // week = Wochenansicht, alles
											// andere ist
	// Monatsansicht
	private String calOpt2 = "";
	private String calOpt3 = "";

	private String conOpt1 = "";
	private String conOpt2 = "";
	private String conOpt3 = "";

	private String toDoOpt1 = "";
	private String toDoOpt2 = "";
	private String toDoOpt3 = "";

	private String mailOpt1 = "";
	private String mailOpt2 = "";
	private String mailOpt3 = "";

	private String language = "";

	private String colorSetting = "";// Farbe --> xxx,xxx,xxx
	private String allOpt3 = "";
	private OptionReader or;
	private String[] windowColorsSeperated;
	private int r;
	private int g;
	private int b;
	private Connection connection;
	private Statement stat;

	// Sprache

	private int speak = 0;

	String[] optionen = { "Optionen", "Options", "Praeferentiae" };
	String[] allgemein = { "Allgemein", "General", "Generalis" };
	String[] kalenderR = { "Kalender", "Calendar", "Calendarium" };
	String[] kontakte = { "Kontakte", "Contacts", "Contactus" };
	String[] aufgaben = { "Aufgaben", "Tasks", "Negotia" };
	String[] mail = { "E-Mail", "Mail", "Epistulae" };
	String[] abbrechen = { "Abbrechen", "Cancel", "Inrita" };
	String[] uebernehmen = { "Übernehmen", "Apply", "Accipere" };

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					OptionsTest frame = new OptionsTest();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public OptionsTest() {

		setTitle(optionen[speak]);
		optionsTestInstance = this;
		or = new OptionReader();
		// OptionReader enthält nach dem TryCatchBlock alle Werte aus der
		// optionsDatei
		try {
			or.getOpionsFromFile();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		// Options aus Reader in die klasse übernehmen
		saveOptionsToClass();

		String windowColors = or.getColorSetting();
		windowColorsSeperated = windowColors.split(",");
		r = Integer.parseInt(windowColorsSeperated[0]);
		g = Integer.parseInt(windowColorsSeperated[1]);
		b = Integer.parseInt(windowColorsSeperated[2]);

		speak = Integer.parseInt(or.getLanguage());

		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(600, 700);
		// setBounds(100, 100, 450, 300);

		contentPane = new JPanel();
		contentPane.setBackground(new Color(r, g, b));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPane, BorderLayout.CENTER);

		final AllgemeinPanel ap = new AllgemeinPanel(getOptionsTestInstance());
		tabbedPane.add(allgemein[speak], ap);
		KalenderOptionPanel kop = new KalenderOptionPanel(
				getOptionsTestInstance());
		tabbedPane.add(kalenderR[speak], kop);
		KontakteOptionPanel koop = new KontakteOptionPanel(
				getOptionsTestInstance());
		tabbedPane.add(kontakte[speak], koop);
		ToDoOptionPanel top = new ToDoOptionPanel(getOptionsTestInstance());
		tabbedPane.add(aufgaben[speak], top);
		MailOptionPanel mop = new MailOptionPanel(getOptionsTestInstance());
		tabbedPane.add(mail[speak], mop);

		JPanel panel = new JPanel();
		panel.setBackground(new Color(r, g, b));
		panel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		panel.setMaximumSize(new Dimension(32767, 40));
		panel.setMinimumSize(new Dimension(10, 40));
		panel.setPreferredSize(new Dimension(10, 40));
		contentPane.add(panel, BorderLayout.SOUTH);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
				0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_panel.rowHeights = new int[] { 0, 0 };
		gbl_panel.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, Double.MIN_VALUE };
		gbl_panel.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
		panel.setLayout(gbl_panel);

		JButton btnbernehmen = new JButton(uebernehmen[speak]);
		btnbernehmen.addActionListener(new ActionListener() {
			@SuppressWarnings("unused")
			public void actionPerformed(ActionEvent e) {

				or.setLanguage(getLanguage());
				or.setColorSetting(ap.getSliderRed().getValue() + ","
						+ ap.getSliderGreen().getValue() + ","
						+ ap.getSliderBlue().getValue());
				or.setAllOpt3(getAllOpt3());
				or.setCalOptStandardView(getCalOptStandardView());
				or.setCalOpt2(getCalOpt2());
				or.setCalOpt3(getCalOpt3());
				or.setConOpt1(getConOpt1());
				or.setConOpt2(getConOpt2());
				or.setConOpt3(getConOpt3());
				or.setMailOpt1(getMailOpt1());
				or.setMailOpt2(getMailOpt2());
				or.setMailOpt3(getMailOpt3());
				or.setToDoOpt1(getToDoOpt1());
				or.setToDoOpt2(getToDoOpt2());
				or.setToDoOpt3(getToDoOpt3());

				ArrayList<String> al = or.returnList();
				String str = or.arrayListToString(al);
				System.out.println("String: " + str);

				connection = tools.createConnection.connect();
				String[] mail = getMailOpt1().split(",");

				try {
					stat = connection.createStatement();
					stat.executeQuery("use `sopra-ws12-awesome_fireballs`;");
					stat.executeUpdate("UPDATE `sopra-ws12-awesome_fireballs`.`Account` SET `email`='"
							+ mail[0]
							+ "' , `imap_server`='"
							+ mail[1]
							+ "' , `smtp_server`='"
							+ mail[3]
							+ "' , `pop3_server`='"
							+ mail[2]
							+ "' , `imap_port`='"
							+ mail[4]
							+ "' , `smtp_port`='"
							+ mail[8]
							+ "' , `pop3_port`='"
							+ mail[5]
							+ "' ,`mail_pass`='"
							+ mail[7]
							+ "' , `provider`='"
							+ mail[6]
							+ "' , `viewtype`='"
							+ getCalOptStandardView()
							+ "', `sort`='"
							+ getConOpt1()
							+ "', `conview`='"
							+ getConOpt2()
							+ "', `sorttodo`='"
							+ getToDoOpt1()
							+ "', `todoview`='"
							+ getToDoOpt2()
							+ "', `language`="
							+ getLanguage()
							+ ", `timeformat`=0, `rcolor`="
							+ r
							+ ", `gcolor`="
							+ g
							+ ", `bcolor`="
							+ b
							+ " WHERE `idUser`="
							+ Test.iduser + ";");
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				OptionWriter ow = new OptionWriter(str);

				dispose();

			}
		});
		btnbernehmen.setMaximumSize(new Dimension(130, 40));
		btnbernehmen.setMinimumSize(new Dimension(130, 40));
		btnbernehmen.setPreferredSize(new Dimension(130, 40));
		GridBagConstraints gbc_btnbernehmen = new GridBagConstraints();
		gbc_btnbernehmen.insets = new Insets(0, 0, 0, 5);
		gbc_btnbernehmen.gridx = 13;
		gbc_btnbernehmen.gridy = 0;
		panel.add(btnbernehmen, gbc_btnbernehmen);

		JButton btnAbbrechen = new JButton(abbrechen[speak]);
		btnAbbrechen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnAbbrechen.setPreferredSize(new Dimension(130, 40));
		btnAbbrechen.setMinimumSize(new Dimension(130, 40));
		btnAbbrechen.setMaximumSize(new Dimension(130, 40));
		GridBagConstraints gbc_btnAbbrechen = new GridBagConstraints();
		gbc_btnAbbrechen.insets = new Insets(0, 0, 0, 5);
		gbc_btnAbbrechen.anchor = GridBagConstraints.EAST;
		gbc_btnAbbrechen.gridx = 14;
		gbc_btnAbbrechen.gridy = 0;
		panel.add(btnAbbrechen, gbc_btnAbbrechen);
	}

	public OptionReader getOr() {
		return or;
	}

	public void setOr(OptionReader or) {
		this.or = or;
	}

	public OptionsTest getOptionsTestInstance() {
		return optionsTestInstance;
	}

	public void setOptionsTestInstance(OptionsTest optionsTestInstance) {
		this.optionsTestInstance = optionsTestInstance;
	}

	/**
	 * Optionen Speichern
	 */
	public void saveOptionsToClass() {
		setLanguage(or.getLanguage());
		setColorSetting(or.getColorSetting());
		setAllOpt3(or.getAllOpt3());
		setCalOptStandardView(or.getCalOptStandardView());
		setCalOpt2(or.getCalOpt2());
		setCalOpt3(or.getCalOpt3());
		setConOpt1(or.getConOpt1());
		setConOpt2(or.getConOpt2());
		setConOpt3(or.getConOpt3());
		setMailOpt1(or.getMailOpt1());
		setMailOpt2(or.getMailOpt2());
		setMailOpt3(or.getMailOpt3());
		setToDoOpt1(or.getToDoOpt1());
		setToDoOpt2(or.getToDoOpt2());
		setToDoOpt3(or.getToDoOpt3());
	}

	public void forceRepaint() {
		paint(getGraphics());
	}

	// Getters & Setters

	public String getStartLine() {
		return StartLine;
	}

	public void setStartLine(String startLine) {
		StartLine = startLine;
	}

	public String getCalOptStandardView() {
		return calOptStandardView;
	}

	public void setCalOptStandardView(String calOptStandardView) {
		this.calOptStandardView = calOptStandardView;
	}

	public String getCalOpt2() {
		return calOpt2;
	}

	public void setCalOpt2(String calOpt2) {
		this.calOpt2 = calOpt2;
	}

	public String getCalOpt3() {
		return calOpt3;
	}

	public void setCalOpt3(String calOpt3) {
		this.calOpt3 = calOpt3;
	}

	public String getConOpt1() {
		return conOpt1;
	}

	public void setConOpt1(String conOpt1) {
		this.conOpt1 = conOpt1;
	}

	public String getConOpt2() {
		return conOpt2;
	}

	public void setConOpt2(String conOpt2) {
		this.conOpt2 = conOpt2;
	}

	public String getConOpt3() {
		return conOpt3;
	}

	public void setConOpt3(String conOpt3) {
		this.conOpt3 = conOpt3;
	}

	public String getToDoOpt1() {
		return toDoOpt1;
	}

	public void setToDoOpt1(String toDoOpt1) {
		this.toDoOpt1 = toDoOpt1;
	}

	public String getToDoOpt2() {
		return toDoOpt2;
	}

	public void setToDoOpt2(String toDoOpt2) {
		this.toDoOpt2 = toDoOpt2;
	}

	public String getToDoOpt3() {
		return toDoOpt3;
	}

	public void setToDoOpt3(String toDoOpt3) {
		this.toDoOpt3 = toDoOpt3;
	}

	public String getMailOpt1() {
		return mailOpt1;
	}

	public void setMailOpt1(String mailOpt1) {
		this.mailOpt1 = mailOpt1;
	}

	public String getMailOpt2() {
		return mailOpt2;
	}

	public void setMailOpt2(String mailOpt2) {
		this.mailOpt2 = mailOpt2;
	}

	public String getMailOpt3() {
		return mailOpt3;
	}

	public void setMailOpt3(String mailOpt3) {
		this.mailOpt3 = mailOpt3;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public String getColorSetting() {
		return colorSetting;
	}

	public void setColorSetting(String colorSetting) {
		this.colorSetting = colorSetting;
	}

	public String getAllOpt3() {
		return allOpt3;
	}

	public void setAllOpt3(String allOpt3) {
		this.allOpt3 = allOpt3;
	}

}
